package com.springbootdf.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springbootdf.Dto.ScoreDto;
import com.springbootdf.common.Result;
import com.springbootdf.entity.Game;
import com.springbootdf.entity.Score;
import com.springbootdf.entity.User;
import com.springbootdf.mapper.ScoreMapper;
import com.springbootdf.service.IScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wy
 * @since 2022-09-29
 */
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private IScoreService scoreService;

    @Autowired
    private ScoreMapper scoreMapper;

    @PostMapping("/returnScore")
    public Result returnScore(@RequestBody Score score){
        return Result.success(scoreService.saveOrUpdateByMultiId(score));
    }

    @GetMapping("/allScore/{gameId}")
    public Result allScore(@PathVariable Integer gameId){
        return Result.success(scoreMapper.allScore(gameId));
    }

//    @GetMapping
//    public IPage<ScoreDto> findPages(@RequestParam(defaultValue = "1") Integer pageNum,
//                                     @RequestParam(defaultValue = "10") Integer pageSize){
//        IPage<ScoreDto> page = new Page<ScoreDto>(pageNum,pageSize);
//
//        return userService.page(page,queryWrapper);
//    }

//    @GetMapping("/page")
//    public Result findGame(@RequestParam(defaultValue = "1") Integer pageNum,
//                           @RequestParam(defaultValue = "5") Integer pageSize
//                            ){
//
//        IPage<ScoreDto> page = scoreService.findPage(new Page<ScoreDto>(pageNum, pageSize));
//        return Result.success(page);
//    }
@GetMapping("/export/{gameId}")
public void export(HttpServletResponse response,@PathVariable Integer gameId) throws Exception {
    // 从数据库查询出所有的数据
    List<ScoreDto> list = scoreMapper.allScore(gameId);
    // 通过工具类创建writer 写出到磁盘路径
//        ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx");
    // 在内存操作，写出到浏览器
    ExcelWriter writer = ExcelUtil.getWriter(true);
    //自定义标题别名
    writer.addHeaderAlias("gameName", "比赛名称");
    writer.addHeaderAlias("userName", "学生/团队名称");
    writer.addHeaderAlias("title", "作品名称");
    writer.addHeaderAlias("allScore", "总分");


    // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
    writer.write(list, true);

    // 设置浏览器响应的格式
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
    String fileName = URLEncoder.encode("成绩信息", "UTF-8");
    response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

    ServletOutputStream out = response.getOutputStream();
    writer.flush(out, true);
    out.close();
    writer.close();

}


}

